package com.example.lizhanqi.livevideo.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.lizhanqi.livevideo.R;
import com.example.lizhanqi.livevideo.adaputer.CommonAdapter;
import com.example.lizhanqi.livevideo.adaputer.ViewHolder;
import com.example.lizhanqi.livevideo.bean.LastedBean;
import com.example.lizhanqi.livevideo.contacts.ContactAddress;
import com.example.lizhanqi.livevideo.presenter.LastedPresenter;
import com.example.lizhanqi.livevideo.ui.activity.ChartVideoRoomActivity;
import com.example.lizhanqi.livevideo.utils.GlideRoundTransform;
import com.example.lizhanqi.livevideo.utils.PhoneUtils;
import com.example.lizhanqi.livevideo.view.ILastedUI;
import com.yolanda.nohttp.Logger;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.RequestQueue;

import java.util.List;

public class LatestFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, ILastedUI {
    private static final String TAG = "李站旗";
    private View view;
    private GridView gridView;
    private SwipeRefreshLayout mSwipeRefreshWidget;
    private RequestQueue requestQueue;
    private LastedPresenter lastedPresenter;
    private CommonAdapter<LastedBean.ContentBean.ListBean> mCommonAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_latest, container, false);
        initView(view);
        lastedPresenter = new LastedPresenter(this);
        requestQueue = NoHttp.newRequestQueue();
        //传入请求的地址
        lastedPresenter.requestGET(ContactAddress.URI, ContactAddress.NEW, requestQueue, 1, false, true);
        return view;
    }

    private void initView(View view) {
        gridView = (GridView) view.findViewById(R.id.gridview);
        int sreccwidth = PhoneUtils.GetScreenWidth(getActivity()) / 2;
        mSwipeRefreshWidget = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_widget);
        mSwipeRefreshWidget.setOnRefreshListener(this);//设置上拉刷新
        // 这句话是为了，第一次进入页面的时候显示加载进度条
        mSwipeRefreshWidget.setRefreshing(true);
    }

    /*上拉动刷新*/
    @Override
    public void onRefresh() {
        //传入请求的地址
        lastedPresenter.requestGET(ContactAddress.URI, ContactAddress.NEW, requestQueue, 1, true, false);
    }

    @Override
    public void succeed(final List<LastedBean.ContentBean.ListBean> llbcl) {
        mSwipeRefreshWidget.setRefreshing(false);
        Toast.makeText(getActivity(), "请求成功", Toast.LENGTH_SHORT).show();
        if (llbcl != null) {
                mCommonAdapter = new CommonAdapter<LastedBean.ContentBean.ListBean>(getActivity(), llbcl, R.layout.show_item_small) {
                    @Override
                    public void convert(ViewHolder holder, LastedBean.ContentBean.ListBean listBean) {
                        holder.setText(R.id.tv_nickname, listBean.getName());
                        holder.setText(R.id.tv_local, listBean.getPlace());
                        holder.setText(R.id.tv_star_level, "   " + listBean.getLevel());
                        holder.setText(R.id.tv_live_number, "LIVE" + listBean.getOnline());
                        ImageView imghead = (ImageView) holder.getView(R.id.img_item_head);
                        Glide.with(getActivity()).load(listBean.getMidheadimg()).transform(new GlideRoundTransform(getActivity(), 10)).placeholder(R.drawable.li_default_head).error(R.drawable.li_default_head).into(imghead);
                        View view = holder.getView(R.id.framelayout_rootview);
                        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                        int sreccwidth = PhoneUtils.GetScreenWidth(getActivity()) / 2;
                        layoutParams.height = sreccwidth;
                        layoutParams.width = sreccwidth;
                        view.setLayoutParams(layoutParams);
                    }
                };
                gridView.setAdapter(mCommonAdapter);
                gridView.deferNotifyDataSetChanged();

        }
        gridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem + visibleItemCount == totalItemCount - 1) {
                    //传入请求的地址
                    lastedPresenter.requestGET(ContactAddress.URI, ContactAddress.NEW, requestQueue, 1, false, false);
                }
            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ChartVideoRoomActivity.class);
                LastedBean.ContentBean.ListBean listBean = llbcl.get(position);
                intent.putExtra("videoPath", listBean.getVideo());
                Logger.i("层  :"+listBean.getVideo());
                startActivity(intent);
            }
        });
    }

    @Override
    public void failed() {
        mSwipeRefreshWidget.setRefreshing(false);
        Toast.makeText(getActivity(), "网络失败", Toast.LENGTH_SHORT).show();
    }
}
